package cn.iocoder.yudao.module.blog.service.jiucase;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucaselevel.BlogJiuCaseLevelDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutagclassity.BlogJiuTagClassityDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.post.BlogPostDO;
import cn.iocoder.yudao.module.blog.service.jiucaselevel.BlogJiuCaseLevelService;
import cn.iocoder.yudao.module.blog.service.jiutag.BlogJiuTagService;
import cn.iocoder.yudao.module.blog.service.jiutagclassity.BlogJiuTagClassityService;
import cn.iocoder.yudao.module.member.api.point.MemberPointApi;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.jiucase.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucase.BlogJiuCaseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.jiucase.BlogJiuCaseConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.jiucase.BlogJiuCaseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 灸法案例 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogJiuCaseServiceImpl implements BlogJiuCaseService {

    @Resource
    private BlogJiuCaseMapper jiuCaseMapper;

    @Resource
    private BlogJiuCaseLevelService blogJiuCaseLevelService;

    @Resource
    private MemberPointApi memberPointApi;

    @Resource
    private BlogJiuTagService blogJiuTagService;

    @Resource
    private BlogJiuTagClassityService blogJiuTagClassityService;

    @Override
    public Long createJiuCase(BlogJiuCaseCreateReqVO createReqVO) {

        Set<Long> tagIds = new HashSet<>();
        if(createReqVO.getTags() != null && StringUtil.isNotEmpty(createReqVO.getTags())) {
            String[] ids = createReqVO.getTags().split(";");
            for (int i = 0; i < ids.length; i++) {
                tagIds.add(Long.parseLong(ids[i]));
            }
        }
        List<BlogJiuTagDO> tagList = blogJiuTagService.getJiuTagList(tagIds);

        Set<Long> tagClassityIds = CollectionUtils.convertSet(tagList, BlogJiuTagDO::getClassity);
        List<BlogJiuTagClassityDO> classityList = blogJiuTagClassityService.getJiuTagClassityList(tagClassityIds);

        List<String> keywordList = new ArrayList<>();
        for(BlogJiuTagDO tag : tagList){
            keywordList.add(tag.getName());
        }

        for(BlogJiuTagClassityDO classity : classityList){
            keywordList.add(classity.getName());
        }

        // 插入
        BlogJiuCaseDO jiuCase = BlogJiuCaseConvert.INSTANCE.convert(createReqVO);
        if(keywordList.size() == 0){
            jiuCase.setKeyword(jiuCase.getTitle());
        }else {
            jiuCase.setKeyword(String.join(";", keywordList));
        }

        jiuCaseMapper.insert(jiuCase);
        // 返回
        return jiuCase.getId();
    }

    @Override
    public void updateJiuCase(BlogJiuCaseUpdateReqVO updateReqVO) {
        // 校验存在
        validateJiuCaseExists(updateReqVO.getId());
        // 更新
        BlogJiuCaseDO updateObj = BlogJiuCaseConvert.INSTANCE.convert(updateReqVO);
        jiuCaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteJiuCase(Long id) {
        // 校验存在
        validateJiuCaseExists(id);
        // 删除
        jiuCaseMapper.deleteById(id);
    }

    private void validateJiuCaseExists(Long id) {
        if (jiuCaseMapper.selectById(id) == null) {
            throw exception(JIU_CASE_NOT_EXISTS);
        }
    }

    @Override
    public BlogJiuCaseDO getJiuCase(Long id) {
        return jiuCaseMapper.selectById(id);
    }

    @Override
    public List<BlogJiuCaseDO> getJiuCaseList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return jiuCaseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogJiuCaseDO> getJiuCasePage(BlogJiuCasePageReqVO pageReqVO) {
        return jiuCaseMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<BlogJiuCaseDO> getJiuCaseAudioPage(BlogJiuCasePageReqVO  pageReqVO){
        return jiuCaseMapper.selectAudioPage(pageReqVO);
    }

    @Override
    public List<BlogJiuCaseDO> getJiuCaseList(BlogJiuCaseExportReqVO exportReqVO) {
        return jiuCaseMapper.selectList(exportReqVO);
    }

    @Override
    public void updateCaseReadNum(Long caseId, Integer value){
        validateJiuCaseExists(caseId);
        BlogJiuCaseDO blogJiuCaseDO = jiuCaseMapper.selectById(caseId);
        blogJiuCaseDO.setReadNum(blogJiuCaseDO.getReadNum() + value);
        jiuCaseMapper.updateById(blogJiuCaseDO);
    }

    @Override
    public void updateCaseLikeNum(Long caseId, Integer value) {
        validateJiuCaseExists(caseId);
        BlogJiuCaseDO blogJiuCaseDO = jiuCaseMapper.selectById(caseId);
        blogJiuCaseDO.setLikeNum(blogJiuCaseDO.getLikeNum() + value);
        jiuCaseMapper.updateById(blogJiuCaseDO);
    }

    @Override
    public void updateCaseCommentNum(Long caseId, Integer value) {
        validateJiuCaseExists(caseId);
        BlogJiuCaseDO blogJiuCaseDO = jiuCaseMapper.selectById(caseId);
        blogJiuCaseDO.setCommentNum(blogJiuCaseDO.getCommentNum() + value);
        jiuCaseMapper.updateById(blogJiuCaseDO);
    }

    @Override
    public void auditCase(Long caseId, Long levelId){
        BlogJiuCaseLevelDO levelDO = blogJiuCaseLevelService.getJiuCaseLevel(levelId);
        if(levelDO == null){
            throw exception(JIU_CASE_LEVEL_NOT_EXISTS);
        }

        BlogJiuCaseDO blogJiuCaseDO = jiuCaseMapper.selectById(caseId);
        if(blogJiuCaseDO == null){
            throw exception(JIU_CASE_NOT_EXISTS);
        }

        if(blogJiuCaseDO.getLevelId() > 0){
            throw exception(JIU_CASE_LEVEL_HAD_AUDIT);
        }

        blogJiuCaseDO.setLevelId(levelId);
        jiuCaseMapper.updateById(blogJiuCaseDO);

        memberPointApi.addPoint(blogJiuCaseDO.getMemberId(), levelDO.getPoint(), MemberPointBizTypeEnum.BLOG_CASE.getType(), String.valueOf(caseId));
    }
}
